package com.yuanqiworld.vw.domain;

import org.apache.commons.io.IOUtils;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.roo.addon.dod.RooDataOnDemand;

import java.io.IOException;
import java.util.*;

@RooDataOnDemand(entity = RoleInfo.class)
public class RoleInfoDataOnDemand {
    public static void main(String[] args) throws IOException {
        // 启动前请将<property name="hibernate.hbm2ddl.auto" value="create"/>.
        new ClassPathXmlApplicationContext("classpath:/META-INF/spring/applicationContext.xml");
        initDB();
    }

    public static void initDB() throws IOException {
        AuthorityInfoDataOnDemand.initDB();
        String json = IOUtils.toString(RoleInfoDataOnDemand.class.getClassLoader().getResource("RoleInfo.json"));
        Collection<RoleInfo> roleInfos = RoleInfo.fromJsonArrayToRoleInfoes(json);
        for (RoleInfo roleInfo : roleInfos) {
            Set<AuthorityInfo> authorityInfos = new HashSet<AuthorityInfo>();
            for (AuthorityInfo authorityInfo : roleInfo.getAuthorities()) {
                authorityInfos.add(AuthorityInfo.findAuthorityInfo(authorityInfo.getId()));
            }
            roleInfo.setAuthorities(authorityInfos);
            roleInfo.merge();
        }
    }
}
